package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.utils.Constant;

public class ActivityFriendsMsgQuery extends BaseFragmentActivity implements
		OnClickListener
{
	private TextView title;
	private ImageButton imgBreak;
	private ImageView imageView;
	private Button addFriends;
	private TextView uName;
	private TextView uSex;
	private TextView uAge;
	private TextView uBirthday;
	private TextView uStatus;
	private TextView uRegDate;
	private TextView uCarNum;
	private TextView uPhone;
	private TextView uSign;
	String Receiver;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_friends_query);
		initViews();
		initEvents();
	}

	@Override
	protected void initViews()
	{
		title = (TextView) super.findViewById(R.id.top_text_middle);
		imgBreak = (ImageButton) super.findViewById(R.id.head_break);
		addFriends = (Button) findViewById(R.id.top_right_but);
		addFriends.setVisibility(View.VISIBLE);
		addFriends.setText("添加");
		imgBreak.setVisibility(View.VISIBLE);
		title.setText("添加好友");
		Intent intent = super.getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		String datas[] = bundle.getStringArray("datas");
		imageView = (ImageView) findViewById(R.id.user_item_iv_avatar);
		uName = (TextView) findViewById(R.id.uname);
		uSex = (TextView) findViewById(R.id.sex);
		uAge = (TextView) findViewById(R.id.age);
		uBirthday = (TextView) findViewById(R.id.time);
		uRegDate = (TextView) findViewById(R.id.regDate);
		uStatus = (TextView) findViewById(R.id.status);
		uCarNum = (TextView) findViewById(R.id.carNum);
		uPhone = (TextView) findViewById(R.id.phone);
		uSign = (TextView) findViewById(R.id.sign);
		// ///////////////////////////////////////////
		imageLoader.displayImage(Constant.POST1 + datas[0], imageView);
		uName.setText(datas[1]);
		uSex.setText("性别\t\t" + datas[2]);
		uSign.setText(datas[3].equals("null") ? "" : datas[3]);
		uAge.setText(datas[4] + "岁");
		uCarNum.setText(datas[5].equals("null") ? "" : datas[5]);
		uBirthday.setText(datas[6].equals("null") ? "" : datas[6]);
		uStatus.setText(datas[7]);
		uRegDate.setText(datas[8]);
		uPhone.setText(datas[9]);
		Receiver = datas[9];
	}

	@Override
	protected void initEvents()
	{
		// TODO Auto-generated method stub
		imgBreak.setOnClickListener(this);
		addFriends.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.top_right_but:
			post();
			break;
		case R.id.head_break:
			finish();
			break;
		}
	}

	public void post()
	{
		showLoadingDialog("请求中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "56");
		params.put("rphone", mApplication.user.getPhone());
		params.put("rname", mApplication.user.getName());
		params.put("rimg", mApplication.user.getIcon());
		params.put("phone", Receiver);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				showShortToast("请求失败");
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				dismissLoadingDialog();
				if (json.contains("000000"))
				{
					showShortToast("好友添加请求已发送");
				}
			}
		});
	}
}
